# -*- coding: utf-8 -*-
import smtplib  
import email.MIMEMultipart# import MIMEMultipart  
import email.MIMEText# import MIMEText  
import email.MIMEBase# import MIMEBase  
import os.path  
import mimetypes  
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib


From = 'wangpengtongxue@163.com' 

#From = 'tonlyautotest@163.com' 
To_all_1 = "w.p@tcl.com"

Tototal_mail=[To_all_1]

file_name = r"D:\eclipse\luyouqi\src\Dlink615result.html"   #Dlink路由器

print '邮箱开始发送'

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))


server = smtplib.SMTP("smtp.163.com",25)   
server.login("wangpengtongxue@163.com","wangpeng123")
 
main_msg = email.MIMEMultipart.MIMEMultipart()  
  

text_msg = email.MIMEText.MIMEText("各位，你好 :     \n  请查收华Polk Audio项目自动化测试报告、\n\n\n\
          此邮件会自动邮件，请勿回复\n\
          备注：由于测试的是路由器，测试完一个路由器需要手动更换一个手机，一个路由器输出一份测试报告，还望谅解（当然也可以汇总合成一份总报告，只是估计耗费很长时间）    ",_charset="utf-8")  


main_msg.attach(text_msg)  

data = open(file_name, 'rb')  
ctype,encoding = mimetypes.guess_type(file_name)  
if ctype is None or encoding is not None:  
    ctype = 'application/octet-stream'  
maintype,subtype = ctype.split('/',1)  
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)  
file_msg.set_payload(data.read())  
data.close( )  
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(file_name)  
file_msg.add_header('Content-Disposition','attachment', filename = basename)
main_msg.attach(file_msg)  
 
main_msg['From'] = _format_addr(u'DQA自动化测试组 <%s>' %From)

#main_msg['Tototal_mail'] = Tototal_mail 
main_msg['Subject'] = "Polk Audio项目自动化测试报告 --Dlink615 "  
main_msg['Date'] = email.Utils.formatdate( )  

fullText = main_msg.as_string( )  

try:  
    for val in Tototal_mail:
        main_msg['val'] = val
        server.sendmail(From, val, fullText)  
finally:  
    server.quit()  
print '邮箱发送结束'